package servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import classes.AccountManager;
import classes.SubjectManager;
import classes.User;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String admin = "0", lecturer = "1",
			sectionLeader = "2", student = "3";
	private static final String lecturerSectionLeader = "12",
			lecturerStudent = "13", lecturerSectionLeaderStudent = "123";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("userName");
		String password = request.getParameter("pass");
		AccountManager manager = (AccountManager) getServletContext()
				.getAttribute(AccountManager.ACCOUNTMANAGER);
		User user = manager.getUser(username);
		if (user == null) {
			RequestDispatcher dispatch = request
					.getRequestDispatcher("LoginPageIncorrect.html");
			dispatch.forward(request, response);
		} else {
			if (manager.isCorrectPassword(username, password)) {
				String status = user.getStatus();
				HttpSession session = request.getSession();
				session.setAttribute("user", user);
				if (status.equals(admin)) {
					RequestDispatcher dispatch = request
							.getRequestDispatcher("adminLogin.jsp");
					dispatch.forward(request, response);
				}
				if (status.equals(lecturer)) {
					RequestDispatcher dispatch = request
							.getRequestDispatcher("lecturerLogin.jsp");
					dispatch.forward(request, response);
				}
				if (status.equals(sectionLeader)) {
					RequestDispatcher dispatch = request
							.getRequestDispatcher("leaderLogin.jsp");
					dispatch.forward(request, response);
				}
				if (status.equals(student)) {
					RequestDispatcher dispatch = request
							.getRequestDispatcher("studentLogin.jsp");
					dispatch.forward(request, response);
				}
				if (status.equals(lecturerSectionLeader)) {
					RequestDispatcher dispatch = request
							.getRequestDispatcher("incorrect.html");
					dispatch.forward(request, response);
				}
				if (status.equals(lecturerStudent)) {
					RequestDispatcher dispatch = request
							.getRequestDispatcher("incorrect.html");
					dispatch.forward(request, response);
				}
				if (status.equals(lecturerSectionLeaderStudent)) {
					RequestDispatcher dispatch = request
							.getRequestDispatcher("incorrect.html");
					dispatch.forward(request, response);
				}
			} else {
				RequestDispatcher dispatch = request
						.getRequestDispatcher("LoginPageIncorrect.html");
				dispatch.forward(request, response);
			}
		}
	}

}
